/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.mpd.musicjukebox.ui.components;

import com.bff.bjj.ui.component.MusicListCellRenderer;
import com.bff.bjj.ui.component.ColorProperties;
import java.awt.Component;
import java.util.HashMap;
import javax.swing.JList;
import org.bff.javampd.objects.MPDSong;

/**
 *
 * @author bfindeisen
 */
public class SongListCellRenderer extends MusicListCellRenderer {

    private HashMap<MPDSong, SongCellPanelIface> songPanels;

    public SongListCellRenderer() {
        super();
        songPanels = new HashMap<MPDSong, SongCellPanelIface>();
    }

    @Override
    public Component getListCellRendererComponent(JList list, 
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        SongCellPanelIface panel = null;
        if (value instanceof MPDSong) {
            MPDSong song = (MPDSong) value;
            panel = songPanels.get(song);

            if (panel == null) {
                panel = new SongCellPanel(song, list);
                songPanels.put(song, panel);
            }
        } else {
            setText(value.toString());
            setColors(list, index, isSelected);
            return this;
        }

        if (panel != null) {
            if ((index % 2) == 0) {
                panel.setEven();
            } else {
                panel.setOdd();
            }

            if (!isSelected) {
                panel.setForeground(ColorProperties.LIST_FOREGROUND);
                panel.setNotSelected();
            } else {
                panel.setSelected();
            }
        }

        return panel.getPanel();
    }
}
